{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【判断】如下定义方式是否正确？\n",
    "   <code>\n",
    "   x=lambda a=1,b=2:a+b   </code>   \n",
    "\n",
    "A. √  \n",
    "B. ×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：A。  \n",
    "解析：lambda的参数可以是默认参数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【判断】lambda函数能以另一个函数的参数形式使用，如filter(lambda x: x % 3 == 0, MyList)。但lambda函数内部的函数返回值部分不能调用其他函数。\n",
    "\n",
    "A. √  \n",
    "B. ×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B。  \n",
    "解析：lambda的函数返回值可以调用其他函数，如add=lambda a,b:ord(a)+ord(b)也是可以的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【编程】利用for循环计算10的阶乘的代码如下，请利用lambda函数简化代码。 \n",
    "   <code> \n",
    "   fac=1  \n",
    "   for i in range(1,11):     \n",
    "       fac*=i  \n",
    "   print(fac)   </code>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案:\n",
    "<code>\n",
    "from functools import reduce   \n",
    "reduce(lambda x,y:x*y, range(1,11))</code>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "4.【编程】请利用lambda函数对列表list=[('b',60),('c',99),('a',80)]以数值从大到小的顺序进行排序。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "答案：\n",
    "<code>\n",
    "list=[('b',60),('c',99),('a',80)]\n",
    "sorted(list,reverse=True,key=lambda n:n[1])</code>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "5.【编程】利用lambda函数获取100以内的质数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "答案：<code>\n",
    "import math\n",
    "list(filter(lambda x: not [x%i for i in range(2, int(math.sqrt(x))+1) if x%i ==0], range(2,101)))</code>"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
